<template>
  <div>
    <el-dialog
      title="填写病历"
      :visible.sync="dialogVisible"
      append-to-body
      width="80%"
      height="50%"
      class="dialog"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :before-close="cancelForm"
      :close-on-click-modal="false"
      center
    >
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="诊断名称" prop="DiagnosisName">
              <el-input
                type="input"
                placeholder="请选择诊断名称"
                size="large"
                @focus="importDiagnosisName"
                v-model="ruleForm.DiagnosisName"
              >
                <el-button
                  slot="append"
                  @click="importDiagnosisName"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="主诉" prop="PatientComplains">
              <el-input
                type="input"
                placeholder="请输入主诉"
                size="large"
                v-model="ruleForm.PatientComplains"
              >
                <el-button
                  slot="append"
                  icon="el-icon-import"
                  @click="importPatientInfo"
                  >导入患者描述信息</el-button
                >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-show="patient_gender == '女'">
            <el-form-item label="怀孕史" prop="PregnantHistory">
              <el-input
                size="large"
                type="input"
                placeholder="请输入怀孕史"
                v-model="ruleForm.PregnantHistory"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="patient_gender == '女'">
            <el-form-item label="生产史" prop="MateranlHistory">
              <el-input
                size="large"
                type="input"
                placeholder="请输入生产史"
                v-model="ruleForm.MateranlHistory"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="patient_gender == '女'">
            <el-form-item label="产子史" prop="MaternalMale">
              <el-input
                size="large"
                type="input"
                placeholder="请输入产子史"
                v-model="ruleForm.MaternalMale"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="patient_gender == '女'">
            <el-form-item label="产女史" prop="MaternalFemale">
              <el-input
                size="large"
                type="input"
                placeholder="请输入怀孕史"
                v-model="ruleForm.MaternalFemale"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="医嘱信息" prop="DoctorAdvice">
              <el-input
                type="textarea"
                placeholder="请输入医嘱信息"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="ruleForm.DoctorAdvice"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <!--        <el-col :span="12">
            <el-form-item prop="sale" label="是否存在患者病史">
              <el-switch
                v-model="ruleForm.is_exist"
                active-value="是"
                inactive-value="否"
              >
              </el-switch>
            </el-form-item>
          </el-col> -->
          <!--  <el-col :span="12">
            <el-form-item label="患者病史" prop="PatientHistory">
              <el-input
                type="input"
                placeholder="请输入患者病史"
                v-model="ruleForm.PatientHistory"
              >
              </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="23" style="padding: 25px">
            <el-divider>患者病史</el-divider>
            <el-button
              class="mb10"
              size="mini"
              type="primary"
              @click="addPatientHis"
              >新 增</el-button
            >
            <el-table
              :data="ruleForm.PatientHistory"
              height="350"
              border
              style="width: 100%"
            >
              <el-table-column label="病史类型" align="center">
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.medicalHistoryType"
                    clearable
                    filterable
                    placeholder="请选择"
                    @change="changeHistoryType($event, scope.row)"
                  >
                    <el-option
                      v-for="item in medicalHistoryTypeArr"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    >
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="详细描述" align="center">
                <template slot-scope="scope">
                  <el-input
                    type="input"
                    placeholder="请输入详细描述"
                    v-model="scope.row.describe"
                  >
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column align="center" width="90">
                <template slot-scope="scope">
                  <i
                  style="font-size:25px;color:#ccc"
                    v-if="scope.$index > 2"
                    class="el-icon-circle-close"
                    @click="
                      handleDeleteRow(scope.$index, ruleForm.PatientHistory)
                    "
                  ></i>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <!--  <el-col :span="12">
            <el-form-item label="病史类型" prop="medicalHistoryType">
              <el-select
                v-model="ruleForm.medicalHistoryType"
                clearable
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in medicalHistoryTypeArr"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!--  <el-col :span="24">
            <el-form-item label="详细描述" prop="describe">
              <el-input
                type="textarea"
                size="medium"
                v-model="ruleForm.describe"
                auto-complete="off"
                placeholder="请输入详细描述"
                :autosize="{ minRows: 5, maxRows: 8 }"
              ></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认</el-button
        >
        <el-button @click="cancelForm('ruleForm')">取消</el-button>
      </div>
      <importDiag ref="importDiag" @submit="submit" />
    </el-dialog>
  </div>
</template>

<script>
import importDiag from "./importDiag.vue";
import { ApiGetOrderInfo, ApiCaseHistoryAdd } from "../../../api/index";
export default {
  components: { importDiag },
  name: "prescribing",
  props: ["id"],
  data() {
    return {
      patient_gender: "",
      select: "",
      tableData: [{ name: "1" }],
      selectArr: [],
      dialogVisible: false,
      oldForm: {
        DiagnosisName: "",
        PatientComplains: "",
        MaritalStatus: "",
        PregnantHistory: "",
        MateranlHistory: "",
        MaternalMale: "",
        MaternalFemale: "",
        DoctorAdvice: "",
        PatientHistory: [
          {
            describe: "",
            medicalHistoryType: "1",
            medicalHistoryTypeStr: "现病史",
          },
          {
            describe: "",
            medicalHistoryType: "4",
            medicalHistoryTypeStr: "过敏史",
          },
          {
            describe: "",
            medicalHistoryType: "5",
            medicalHistoryTypeStr: "既往史",
          },
        ],
      },
      ruleForm: {},
      list: [
        {
          id: "1",
          name: "鼠疫",
          code: "SY",
          checked: false,
        },
        {
          id: "2",
          name: "炭疽",
          code: "TJ",
          checked: false,
        },
        {
          id: "3",
          name: "鼻疽",
          code: "BJ",
          checked: false,
        },
        {
          id: "4",
          name: "麻风",
          code: "MF",
          checked: false,
        },
        {
          id: "5",
          name: "伤寒",
          code: "SH",
          checked: false,
        },
        {
          id: "6",
          name: "霍乱",
          code: "HL",
          checked: false,
        },
        {
          id: "7",
          name: "肾结核",
          code: "SJH",
          checked: false,
        },
        {
          id: "8",
          name: "肠结核",
          code: "CJH",
          checked: false,
        },
        {
          id: "9",
          name: "眼结核",
          code: "YJH",
          checked: false,
        },
        {
          id: "10",
          name: "舌结核",
          code: "SJH",
          checked: false,
        },
      ],
      MaritalStatusArr: [
        { name: "未婚", code: "0" },
        { name: "已婚", code: "1" },
        { name: "其他未知", code: "2" },
      ],
      //病史类型
      medicalHistoryTypeArr: [
        {
          name: "现病史",
          code: "1",
        },
        {
          name: "月经史",
          code: "2",
        },
        {
          name: "流产史",
          code: "3",
        },
        {
          name: "过敏史",
          code: "4",
        },
        {
          name: "既往史",
          code: "5",
        },
        {
          name: "家族史",
          code: "6",
        },
        {
          name: "手术外伤史",
          code: "7",
        },
        {
          name: "传染病接触史",
          code: "8",
        },
        {
          name: "流行病学史",
          code: "9",
        },
      ],
      /*  rules: {
         zhanduan: [
           { required: true, message: '请输入诊断', trigger: 'change' }
         ],
         zhusu: [
           { required: true, message: '请输入主诉', trigger: 'change' }
         ],
         xianbingshi: [
           { required: true, message: '请输入现病史', trigger: 'change' }
         ],
         guominshi: [
           { required: true, message: '请输入过敏史', trigger: 'change' }
         ],
         jiwangshi: [
           { required: true, message: '请输入既往史', trigger: 'change' }
         ],
         tigejiancha: [
           { required: true, message: '请输入体格检查', trigger: 'change' }
         ],
         yijian: [
           { required: true, message: '请输入意见', trigger: 'change' }
         ],
       } */
    };
  },
  methods: {
    submit(e, val) {
      this.ruleForm.DiagnosisName = e.toString();
      this.ruleForm.DiagnosisCode = val.toString();
    },
    //诊断疾病进行选择
    importDiagnosisName() {
      this.$refs.importDiag.show();
    },
    //翻译
    changeHistoryType(e, row) {
      let val = this.medicalHistoryTypeArr.find((item) => {
        if (item.code == e) {
          return item;
        }
      });
      if (val) {
        row.medicalHistoryTypeStr = val.name;
      }
    },
    handleDeleteRow(index, rows) {
      rows.splice(index, 1);
    },
    cancelForm() {
      this.oldForm.PatientHistory = [
        {
          describe: "",
          medicalHistoryType: "1",
          medicalHistoryTypeStr: "现病史",
        },
        {
          describe: "",
          medicalHistoryType: "4",
          medicalHistoryTypeStr: "过敏史",
        },
        {
          describe: "",
          medicalHistoryType: "5",
          medicalHistoryTypeStr: "既往史",
        },
      ];
      this.dialogVisible = false;
    },

    show(val) {
      console.log(val);
      this.patient_gender = val;
      const data = this.oldForm;
      for (const key in this.oldForm) {
        this.$set(this.ruleForm, key, data[key]);
      }
      this.dialogVisible = true;
    },
    addPatientHis() {
      var addObj = {
        describe: "",
        medicalHistoryType: "",
      };
      this.ruleForm.PatientHistory.push(addObj);
    },
    getSelectArr(val) {
      this.selectArr = val;
      let arr = [];
      val.forEach((item) => {
        let val = item.name + "-" + item.price;
        arr.push(val);
      });
      this.ruleForm.prescribeMedicine = arr.toString();
    },
    //导入患者描述信息
    /*  importPatientInfo() {
 
     } */
    importPatientInfo() {
      ApiGetOrderInfo(this.id).then((res) => {
        this.ruleForm.PatientComplains = res.data.patient_status;
      });
    },

    submitForm(val) {
      ApiCaseHistoryAdd({
        ...this.ruleForm,
        PatientHistory: JSON.stringify(this.ruleForm.PatientHistory),
        order_id: this.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("添加成功");
          this.cancelForm();
        }
      });
    },
    //开药
    medicineClick() {
      /*    this.$refs.medicine.show() */
    },
  },
};
</script>

<style scoped>
</style>